package view
{
	import com.bit101.components.HBox;
	import com.bit101.components.ImageButton;
	import com.bit101.components.NavBtn;
	import com.bit101.components.PushButton;
	import com.bit101.components.VBox;
	import com.liu.load.LoadInfo;
	import com.liu.load.LoadManager;
	
	import events.CategoryEvent;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import models.ModelLocator;
	
	public class CategoryBtns extends Sprite
	{
		private var bntAry:Array;
		private var leftBox:VBox;
		private var topBox:HBox
		private var flagSp:Shape;
		public function CategoryBtns(url:String)
		{
			initBox();
			var loadinfo:LoadInfo = new LoadInfo(url,LoadInfo.XML,initXML);
			LoadManager.getInstance().addSingleLoad(loadinfo);
		}
		private function initBox():void{
			leftBox = new VBox;
			leftBox.spacing = 15;
			leftBox.y = 20;
			this.addChild(leftBox);
			
			topBox = new HBox;
			topBox.spacing = 10;
			this.addChild(topBox);
			topBox.x = 170;
			
			flagSp = new Shape;
			flagSp.graphics.beginFill(0xFF7F00);
			flagSp.graphics.drawCircle(0,0,3);
			flagSp.graphics.endFill();
			this.addChildAt(flagSp,0);
		}
		private function initXML(myxml:XML):void{
			bntAry = new Array;
			for each(var classify:XML in myxml.classify){
				var ary:Array = new Array;
				for each(var prop:XML in classify.item){
					var obj:Object = new Object;
					//obj.w = int(prop.@w);
					//obj.h = int(prop.@h);
					obj.url = String(prop.@url);
					obj.id = int(prop.@id);
					var newary:Array = new Array;
					for each(var p:XML in prop.btn){
						var newObj:Object = new Object;
						newObj.url = String(p.@url);
						newObj.id = int(p.@id);
						newary.push(newObj);
					}
					obj.data = newary;
					ary.push(obj);
				}
				var classObj:Object = new Object;
				classObj.data = ary;
				classObj.w = int(classify.@w);
				classObj.h = int(classify.@h);
				classObj.url = String(classify.@url);
				classObj.id = int(classify.@id);
				bntAry.push(classObj);
			}
			initdefaultBtn();
		}
		
		private function initdefaultBtn():void{
			for(var i:int;i<bntAry.length;i++){
				var imgbtn:ImageButton = new ImageButton(leftBox,0,0,bntAry[i].w,bntAry[i].h,"assets/btns/" +bntAry[i].url,onClick);
				imgbtn.data = bntAry[i];
			}
			var obj:Array = bntAry[0].data as Array;
			var btn:NavBtn;
			for(var j:int=0;j<obj.length;j++){
				btn = new NavBtn(topBox,0,0,0,0,"assets/btns/" +obj[j].url,null,obj[j]);
				//btn.data = obj[j];
				//btn.setDropList(obj[j].data);
				//imgbtn.y = (60 - obj[j].h)/2;
				if(j==0){
					btn.first = true;
				}
				btn.addEventListener(Event.SELECT_ALL,onselect);
			}
			if(obj.length > 0){
				var evt:CategoryEvent = new CategoryEvent(CategoryEvent.CATEGORYEVENT);
				evt.data = obj[0].data[0];
				this.dispatchEvent(evt);
			}
			//flagSp.x = topBox.x + NavBtn(event.target).x + NavBtn(event.target).width/2;
			//flagSp.y = topBox.y + NavBtn(event.target).height - NavBtn(event.target).btnList.height + 10;
		}
		private function onClick(event:MouseEvent):void{
			topBox.removeAllChild();
			var obj:Array = event.currentTarget.data.data as Array;
			for(var j:int=0;j<obj.length;j++){
				var btn:NavBtn = new NavBtn(topBox,0,0,0,0,"assets/btns/" +obj[j].url,null,obj[j]);
				//btn.data = obj[j];
				//btn.setDropList(obj[j].data);
				//imgbtn.y = (60 - obj[j].h)/2;
				if(j==0){
					btn.first = true;
				}
				btn.addEventListener(Event.SELECT_ALL,onselect);
			}
			ModelLocator.getInstance().init = false;
			Main(this.parent).clear();
			if(obj.length > 0){
				var evt:CategoryEvent = new CategoryEvent(CategoryEvent.CATEGORYEVENT);
				evt.data = obj[0].data[0];
				this.dispatchEvent(evt);
			}
		}
		private function onselect(event:Event):void{
			flagSp.x = topBox.x + NavBtn(event.target).x + NavBtn(event.target).width/2;
			flagSp.y = topBox.y + NavBtn(event.target).height - NavBtn(event.target).btnList.height + 10;
		}
		private function itemClick(event:MouseEvent):void{
			var obj:Object = event.currentTarget.data;
			var evt:CategoryEvent = new CategoryEvent(CategoryEvent.CATEGORYEVENT);
			evt.data = obj;
			this.dispatchEvent(evt);
		}
	}
}